﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Supervise_TraPayment : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    public string _strEndDate, _strStartDate, _AreaId = "", _strUserType = "", _strDisplay = "", _strAccountId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _AreaId = SysFun.ToTrim(_Account.AreaId);
                _strUserType = SysFun.ToTrim(_Account.Type);
                _strAccountId = SysFun.ToTrim(_Account.AccountId);
                //如果当前使用人是配送企业
                if (_strUserType == "2")
                {
                    chkUserPS.Visible = false;
                    lab1.Visible = false;
                    _AreaId = "530000";
                }

            }
            if (_strUserType == "3")
            {
                chkUserYY.Visible = false;
                _strDisplay = "none";

                //如果当前用户是医院，那么就把地区隐藏
                labArea.Visible = false;
                Label1.Visible = false;
            }
            else
            {
                chkUserYY.Visible = true;
                _strDisplay = "block";
                //如果是监督机构或中介就根据其所在的地区来选择
                //如果是省一级的监督机构就可以看到整个云南省的信息,如果是某个市的监督机构就只能看到本市的信息
                System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
                string _strAreaNameForm, _strAreaIdForm;
                _strAreaNameForm = SysFun.IsSQL(SysFun.ToTrim(Request.Form["txtAreaName"]));
                _strAreaIdForm = SysFun.IsSQL(SysFun.ToTrim(Request.Form["txtAreaId"]));
                if (_strAreaNameForm != "" && _strAreaIdForm != "")
                {
                    strHtml.Append("<input type='text' name='txtAreaName' value ='" + _strAreaNameForm + "' id='Text1' language='javascript' onclick='return Text1_onclick()'  InvertedComma='所属地区' readonly />");
                    strHtml.Append("<input type='text' name='txtAreaId'  value ='" + _strAreaIdForm + "' style='display: none' />");
                }
                else
                {
                    strHtml.Append("<input type='text' name='txtAreaName' id='Text1' language='javascript' onclick='return Text1_onclick()'  InvertedComma='所属地区' readonly />");
                    strHtml.Append("<input type='text' name='txtAreaId' style='display: none' />");
                }
                if (_AreaId != "")
                {
                    strHtml.Append("<a href='javascript:AreaSelect(" + _AreaId + ")'>选择地区</a>");
                }
                labArea.Text = strHtml.ToString();
                labArea.Visible = true;
                Label1.Visible = true;
            }
            if (!IsPostBack)
            {
                int Year, Month, Day, DMonth, Year1;
                Year = DateTime.Now.Year;
                Year1 = Year;
                DMonth = DateTime.Now.Month;
                Month = DMonth - 1;
                if (Month == 0)
                {
                    Year1 -= 1;
                    Month = 12;
                }

                Day = DateTime.Now.Day;
                txtStartDate.Text = Year1.ToString() + "-" + Month.ToString() + "-01";
                txtEndDate.Text = Year.ToString() + "-" + DMonth.ToString() + "-01";
                
            }

            if (Request.Form["butSelectAll"] == "查看全部")
            {

                string strSql = @"select  PaymentId,PaymentName,AccountId_YY,HospitalName,UserCode_YY,UserCode_PS, AccountId_PS,CompanyName_PS,PaymentStartDate,PaymentEndDate
                                ,convert(numeric(20,2),PaymentPrice) as HKJE,convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE
                                from traPayment where  {0}  group by  PaymentId,PaymentName,AccountId_YY,HospitalName,UserCode_YY,UserCode_PS, AccountId_PS,CompanyName_PS,PaymentStartDate,PaymentEndDate,PaymentPrice order by HKJE desc,PaymentId";
                string _strtempWhere = fctConditions();
                strSql = String.Format(strSql, _strtempWhere);
                Db db1 = new Db();
                System.Data.IDataReader dr;
                dr = db1.DataReader(strSql);
                GridView1.DataSource = dr;
                GridView1.DataBind();
                dr.Close();
                dr.Dispose();
                db1.Close();
            }
            else
            {
                showGridView();
            }

        }
    }
    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
          
            //<asp:TemplateField HeaderText="结算时间范围">
            //    <ItemTemplate>
            //        <%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "PaymentStartDate")).ToShortDateString()%>
            //        --<br><%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "PaymentEndDate")).ToShortDateString()%>
            //    </ItemTemplate>
            //</asp:TemplateField>
          
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
       // dbPage.Table = "TraPayment_2008"; 
        dbPage.Table = "TraPayment";//要查询的表明
        dbPage.SelectFieldGroup = "ispayment,PaymentId,PaymentName,AccountId_YY,HospitalName,UserCode_YY,UserCode_PS, AccountId_PS,CompanyName_PS,PaymentPrice,convert(varchar(100),hospitalinflowdate,23),convert(varchar(100),paynamtdate,23),Datediff(dd,hospitalinflowdate,paynamtdate)";
        dbPage.Key = "PaymentId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "ispayment,PaymentId,PaymentName,AccountId_YY,HospitalName,UserCode_YY,UserCode_PS, AccountId_PS,CompanyName_PS,convert(numeric(20,2),PaymentPrice) as HKJE,convert(varchar(100),hospitalinflowdate,23) as hospitalinflowdate,convert(varchar(100),paynamtdate,23) as paynamtdate,Datediff(dd,hospitalinflowdate,paynamtdate) as Cha";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;                                                    //没有显示记录条数   
        dbPage.OrderBy = "HKJE,PaymentId";                  //排序字段
        dbPage.OrderByIsDesc = true;
        dbPage.HKJE = "convert(numeric(20,2),sum(PaymentPrice)) as HKJE";
        dbPage.DHJE = "convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE";
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string _strTemp = " DealStatus=2 and", _strPaymentId = "", _strPaymentName, _strAreaIdForm,_strispayment;

        //配送企业
        string _strAccountIdPS = SysFun.ToTrim(Request.Form["txtAccountIdPS"]);
        if (_strAccountIdPS != "" && chkUserPS.Checked == true)
        {
            _strTemp += " AccountId_PS =" + _strAccountIdPS + " and";
        }

        //时间范围
        _strEndDate = txtEndDate.Text.ToString();
        _strStartDate = txtStartDate.Text.ToString();
        if (_strEndDate != "" && _strStartDate != "")
        {
            _strTemp += " hospitalinflowdate >='" + _strStartDate + "' and hospitalinflowdate <='" + _strEndDate + "' and";
        }
        //如果当前使用人是配送企业
        if (_strUserType == "2")
        {
            _strTemp += " AccountId_PS=" + _strAccountId + " and";
        }


        //根据登录人和地区确定医院范围
        if (_strUserType == "3")
        {
            _strTemp += "  AccountId_YY=" + _strAccountId + " and";
        }
        else
        {
            string _strAccountIdYY1 = SysFun.ToTrim(Request.Form["txtAccountIdYY"]);
            if (_strAccountIdYY1 != "" && chkUserYY.Checked == true)
            {
                _strTemp += " AccountId_YY =" + _strAccountIdYY1 + " and";
            }
            _strAreaIdForm = SysFun.ToTrim(Request.Form["txtAreaId"]);
            if (_strAreaIdForm != "" && _strAreaIdForm != "530000")
            {
                _AreaId = _strAreaIdForm;
            }
            _strTemp += SysFun.AreaIdWhere(_AreaId);
        }


        //结算单号
        _strPaymentId = txtPaymentId.Text.ToString();
        if (_strPaymentId != "")
        {
            _strTemp += "  PaymentId =" + _strPaymentId + " and";
        }

        //结算单名称
        _strPaymentName = txtPaymentName.Text.ToString();
        if (_strPaymentName != "")
        {
            _strTemp += "  PaymentName like '%" + _strPaymentName + "%' and";
        }
        //是否结算
        _strispayment = DropDownList1.Text;
        if (_strispayment == "已确认")
        {
            _strTemp += " ispayment =1 and";
        }
        else
        {
            _strTemp += " isnull(ispayment,0) <>1 and";
        }
        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }
        return _strTemp;
    }

  

    protected void ToEcxul_Click(object sender, EventArgs e)
    {
        Db db = new Db();
        System.Data.IDataReader dr;
        string _strSql = "Select  PaymentId as 结算单号,PaymentName as 结算单名称,HospitalName as 医院名称,UserCode_YY as 医院注册号,CompanyName_PS as 配送企业名称,UserCode_PS as 配送企业注册号,PaymentStartDate as 开始时间 ,PaymentEndDate as 结束时间,convert(numeric(20,2),PaymentPrice) as '结算金额（元）',convert(numeric(20,2),sum(inflownum*TradePrice)) as '到货金额（元）'  From traPayment Where     " + fctConditions() + "  Group By PaymentId,PaymentName,AccountId_YY,HospitalName,UserCode_YY,UserCode_PS, AccountId_PS,CompanyName_PS,PaymentStartDate,PaymentEndDate,PaymentPrice Order By '结算金额（元）' Desc,PaymentId Desc";

        dr = db.DataReader(_strSql);
        this.GridView2.DataSource = dr;
        this.GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        SysFun.WriteToExcel(GridView2, this.Page, "按结算单查询.xls");


    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }
    #region TraDes 成员

    public string Name
    {
        get { return "按回款单查询"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "交易查询"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSuperviseChildCheck);
            return _AccountType;
        }
    }
    #endregion




    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (e.Row.Cells[8].Text.Trim() == "&nbsp;")
            {
                e.Row.BackColor = System.Drawing.Color.Khaki;
                return;
            }
            if (Convert.ToInt32(e.Row.Cells[8].Text) > 60)
            {
                e.Row.BackColor = System.Drawing.Color.Red;
            }
        }
    }
}
